﻿
/*----------------------------------------------------------------
    // Copyright (C) 2009 江苏华招网
    // 版权所有。 
    // 作者：庄金冬 
    //
    // 文件名：BLL_Area.cs
    // 文件功能描述：地区操作业务逻辑类,实现接口IBLL_Area,继承自类MarshalByRefObject
    // 创建标识：庄金冬2009-03-06
    //
    // 修改标识：
    // 修改描述：
    //
    // 修改标识：
    // 修改描述：
    //
    // 审核人：
    // 审核时间：
    // 审核说明：
 //----------------------------------------------------------------*/
using System;
using System.Collections.Generic;

using System.Text;
using HZ.DrugTrade.IBLL;
using HZ.DrugTrade.Model;
using HZ.DrugTrade.DAL;
using System.Data;
namespace HZ.DrugTrade.BLL
{

    [Serializable]
    /// <summary>
    /// 地区操作业务逻辑类,实现接口IBLL_Area,继承自类MarshalByRefObject
    /// </summary>
    public class BLL_Area : MarshalByRefObject, IBLL_Area
    {
        #region IBLL_Area 成员
        /// <summary>
        /// 获取所有区域信息
        /// </summary>
        /// <returns>区域信息集合</returns>
        public IList<Area> GetAllArea()
        {
            int totalRecord = 0;
            Area area = new Area();
            return DAL_Area.GetCompanyByWhere(area, 1, 0, out totalRecord);
        }

        /// <summary>
        /// 获取所有省级区域信息
        /// </summary>
        /// <returns>区域信息集合</returns>
        public IList<Area> GetAllProvince()
        {
            int totalRecord = 0;
            Area area = new Area()
            {
                FatherAreaID = "0"
            };
            return DAL_Area.GetCompanyByWhere(area, 1, 0, out totalRecord);
        }

        /// <summary>
        /// 根据省级编号获取所有县区
        /// </summary>
        /// <param name="ProvinceAreaID">省级（含同级【如：直辖市，自治区等】）编号</param>
        /// <returns>区域信息集合</returns>
        public IList<Area> GetAllCity(string ProvinceAreaID)
        {
            int totalRecord = 0;
            Area area = new Area()
            {
                FatherAreaID = ProvinceAreaID
            };
            return DAL_Area.GetCompanyByWhere(area, 1, 0, out totalRecord);
        }

        /// <summary>
        /// 根据市级编号获取所有县区
        /// </summary>
        /// <param name="CityAreaID">市级编号</param>
        /// <returns>区域信息集合</returns>
        public IList<Area> GetAllCounty(string AreaID)
        {
            int totalRecord = 0;
            Area area = new Area()
            {
                FatherAreaID = AreaID
            };
            return DAL_Area.GetCompanyByWhere(area, 1, 0, out totalRecord);
        }

        /// <summary>
        /// 根据地区编号获取地区信息
        /// </summary>
        /// <param name="AreaID">地区编号</param>
        /// <returns>区域信息</returns>
        public Area GetArea(string AreaID)
        {
           
            int totalRecord = 0;
            Area area = new Area()
            {
                AreaID = AreaID
            };
            IList<Area> AreaList = DAL_Area.GetCompanyByWhere(area, 1, 0, out totalRecord);
            if (AreaList.Count > 0)
            {
                area = DAL_Area.GetCompanyByWhere(area, 1, 0, out totalRecord)[0];
            }
            return area;
        }
        /// <summary>
        /// 获取当前实施地区
        /// </summary>
        /// <returns></returns>
        //public Area GetCurrentArea()
        //{
        //    //省级代码，例如：江苏省 320000 
        //    return GetArea("320000");
        //}
        #endregion

        /// <summary>
        /// 根据地区编号获取地区名称
        /// </summary>
        /// <param name="AreaId"></param>
        /// <returns></returns>
        public string GetAreaName(string AreaId)
        {
            string sql = string.Format(@"select a.areaname+' '+b.areaname from (
                                    select * from tb_area where areaid  in (select fatherareaid from tb_area where areaid = '{0}')) a
                                    left join (
                                    select * from tb_area where areaid = '{0}') b 
                                    on a.areaid = b.fatherareaid", AreaId);
            var areaname = HZ.Data.DbHelperFactory.Create().ExecuteScalar(sql);
            return areaname == null ? "" : areaname.ToString();
        }

        /// <summary>
        /// 获取未选择的区域 by zdc
        /// </summary>
        /// <param name="where"></param>
        /// <returns></returns>
        public IList<Area> GetUnChoseArea(string where)
        {
            return DAL_Area.GetUnChoseArea(where);
        }
        /// <summary>
        /// 增加配送企业选择的地区 by zdc
        /// </summary>
        /// <param name="areaIds">地区</param>
        /// <param name="userid">企业id</param>
        /// <returns>return true or flase</returns>
        public bool AddChoseArea(List<string> areaIds, string userid)
        {
            bool state = DAL_Area.AddChoseArea(areaIds, userid);
            if(state == true)
            {
                BLL_Log.Instance.AddKeyOperationLog(new KeyOperationLog { OperateMessage = string.Format("增加配送企业选择的地区：{0};处理时间:{1};)", userid, DateTime.Now.ToString()), ClientIP = HZ.Fun2.GetClientIP() });
            }
            return state;
        }
        /// <summary>
        /// 根据配送企业id获取已选择的地区 by zdc
        /// </summary>
        /// <param name="userid">企业id</param>
        /// <returns>return datatable</returns>
        public DataTable GetSelectArea(string userid, string projectid)
        {
            return DAL_Area.GetSelectArea(userid, projectid);
        }
        /// <summary>
        /// 处理已选择的区域 by zdc
        /// </summary>
        /// <param name="areaIds">地区id</param>
        /// <param name="type">类型：1提交 否则删除</param>
        /// <returns>return true or false</returns>
        public bool DealSelectArea(string areaIds, int type,string userid)
        {
            bool result = DAL_Area.DealSelectArea(areaIds, type, userid);
            if (result) {

                BLL_Log.Instance.AddKeyOperationLog(new KeyOperationLog() { OperateMessage = string.Format("{0}用户确认删除", userid) }); 
            }
            return result;
        }
        /// <summary>
        /// 获取要在本地区配送的企业 by zdc
        /// </summary>
        /// <param name="where"></param>
        /// <param name="currentPage"></param>
        /// <param name="pageSize"></param>
        /// <param name="totalRecord"></param>
        /// <returns></returns>
        public DataTable GetComById(string where, int currentPage, int pageSize, out int totalRecord)
        {
            return DAL_Area.GetComById(where, currentPage, pageSize, out totalRecord);
        }

        /// <summary>
        /// 获取要在本地区配送的企业 by zdc
        /// </summary>
        /// <param name="where"></param>
        /// <param name="currentPage"></param>
        /// <param name="pageSize"></param>
        /// <param name="totalRecord"></param>
        /// <returns></returns>
        public DataTable GetComByIdMS(string where, int currentPage, int pageSize, out int totalRecord)
        {
            return DAL_Area.GetComByIdMS(where, currentPage, pageSize, out totalRecord);
        }
        /// <summary>
        /// 审核在本地区备案的企业 By zdc
        /// </summary>
        /// <param name="companyid"></param>
        /// <param name="checkType"></param>
        /// <returns></returns>
        public bool CheckEnterPrise(string companyid, int checkType, string userId)
        {
            bool state = DAL_Area.CheckEnterPrise(companyid, checkType, userId);
            if(state == true)
            {
                BLL_Log.Instance.AddKeyOperationLog(new KeyOperationLog { OperateMessage = string.Format("审核在本地区备案的企业：{0};处理时间:{1};)", companyid, DateTime.Now.ToString()), ClientIP = HZ.Fun2.GetClientIP() });
            }
            return state;
        }
        /// <summary>
        /// 内蒙招标中心（zbzx）审核备案的企业 by zdc
        /// </summary>
        /// <param name="companyIds"></param>
        /// <param name="areaIds"></param>
        /// <param name="checkType"></param>
        /// <returns></returns>
        public bool CheckAllEnterPrise(string companyIds, string areaIds, int checkType)
        {
            bool state = DAL_Area.CheckAllEnterPrise(companyIds, areaIds, checkType);
            if(state == true)
            {
                BLL_Log.Instance.AddKeyOperationLog(new KeyOperationLog { OperateMessage = string.Format("内蒙招标中心（zbzx）审核备案的企业：{0};处理时间:{1};)", companyIds, DateTime.Now.ToString()), ClientIP = HZ.Fun2.GetClientIP() });
            }
            return true;
        }

        /// <summary>
        /// 根据企业的id获取所有备案审核通过的地区 by zdc
        /// </summary>
        /// <param name="CompanyPS_Id"></param>
        /// <returns></returns>
        public string GetNeedCity(string CompanyPS_Id, string projectid)
        {
            return DAL_Area.GetNeedCity(CompanyPS_Id, projectid);
        }
        /// <summary>
        /// 根据条件获取区域 by zdc
        /// </summary>
        /// <param name="companyids"></param>
        /// <param name="type"></param>
        /// <returns></returns>
        public string GetAreasByWhere(string companyids, int type)
        {
            return DAL_Area.GetAreasByWhere(companyids, type);
        }
        /// <summary>
        /// 中心端查询企业配送区域 add by Meng 2013-07-01
        /// </summary>
        /// <param name="where"></param>
        /// <param name="currentPage"></param>
        /// <param name="pageSize"></param>
        /// <param name="totalRecord"></param>
        /// <returns></returns>
        public DataTable GetCompanyAreaidByCompanyid(string where, int currentPage, int pageSize, out int totalRecord)
        {
            return DAL_Area.GetCompanyAreaidByCompanyid(where, currentPage, pageSize, out totalRecord);
        }
          /// <summary>
        /// 中心端编辑配送区域 add by Meng 2013-07-01
        /// </summary>
        /// <param name="areaIds">地区</param>
        /// <param name="userid">企业id</param>
        /// <returns>return true or flase</returns>
        public bool UpdateComapnyAreaByCompanyid(List<string> areaIds, string userid)
        {
            bool state = DAL_Area.UpdateComapnyAreaByCompanyid(areaIds, userid);
            if (state == true)
            {
                BLL_Log.Instance.AddKeyOperationLog(new KeyOperationLog { OperateMessage = string.Format("中心端为配送企业新增配送区域：{0} 处理人：{2};处理时间:{1};)", userid, DateTime.Now.ToString(), BLL_UserSession.GetCurrentUser().UserID), ClientIP = HZ.Fun2.GetClientIP() });
            }
            return state;
        }
        /// <summary>
        /// 中心端获取未选择和未提交的区域 by Meng 2013-07-04
        /// </summary>
        /// <param name="where"></param>
        /// <returns></returns>
        public IList<Area> GetUnChoseAreaByZBZX(string where)
        {
            return DAL_Area.GetUnChoseAreaByZBZX(where);
        }
    }
}
